<?php

// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------

namespace app\adminapi\logic\user;


use app\common\model\user\DriverCredential;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * DriverCredential逻辑
 * Class DriverCredentialLogic
 * @package app\adminapi\logic\user
 */
class DriverCredentialLogic extends BaseLogic
{
    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/25 16:46
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            if($params['car_pic'])
            {
                $params['car_pic'] = json_encode($params['car_pic']);
            }
            DriverCredential::create([
                'user_id' => $params['user_id'],
                'id_card' => $params['id_card'],
                'driver_license_pic' => $params['driver_license_pic'],
                'driver_license_valid_date' => $params['driver_license_valid_date'],
                'vehicle_license_pic' => $params['vehicle_license_pic'],
                'vehicle_license_valid_date' => $params['vehicle_license_valid_date'],
                'car_num' => $params['car_num'],
                'network_license_pic' => $params['network_license_pic'],
                'network_licensee_pic' => $params['network_licensee_pic'],
                'car_pic' => $params['car_pic'],
                'car_type_id' => $params['car_type_id'],
                'status' => $params['status'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/25 16:46
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            if($params['car_pic'])
            {
                $params['car_pic'] = json_encode($params['car_pic']);
            }
            DriverCredential::where('id', $params['id'])->update([
                'user_id' => $params['user_id'],
                'id_card' => $params['id_card'],
                'driver_license_pic' => $params['driver_license_pic'],
                'driver_license_valid_date' => $params['driver_license_valid_date'],
                'vehicle_license_pic' => $params['vehicle_license_pic'],
                'vehicle_license_valid_date' => $params['vehicle_license_valid_date'],
                'car_num' => $params['car_num'],
                'network_license_pic' => $params['network_license_pic'],
                'network_licensee_pic' => $params['network_licensee_pic'],
                'car_pic' => $params['car_pic'],
                'car_type_id' => $params['car_type_id'],
                'status' => $params['status'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/25 16:46
     */
    public static function delete(array $params): bool
    {
        return DriverCredential::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/25 16:46
     */
    public static function detail($params): array
    {
        return DriverCredential::findOrEmpty($params['id'])->toArray();
    }
}